﻿<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema"
        xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
        version="1.0"
        exclude-result-prefixes="xsl msxsl ddwrt"
        xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
        xmlns:asp="http://schemas.microsoft.com/ASPNET/20"
        xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt"
        xmlns:SharePoint="Microsoft.SharePoint.WebControls"
        xmlns:ddwrt2="urn:frontpage:internal">
	<xsl:template match ="FieldRef[@FieldType='LastViewedField']" mode="Text_body">
		<xsl:param name="thisNode" select="."/>
		<xsl:choose>
			<xsl:when test="$thisNode/@*[name()=current()/@Name]=''">
				<span>
					<xsl:attribute name="NullLastViewedId">
						<xsl:value-of select="$thisNode/@ID"/>
					</xsl:attribute>
					Refreshing...
				</span>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$thisNode/@*[name()=current()/@Name]"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match ="FieldRef[@FieldType='LastViewedField']" ddwrt:dvt_mode="header" mode="header">
		<th nowrap="nowrap" scope="col" onmouseover="OnChildColumn(this)">
			<xsl:attribute name="class">
				<xsl:choose>
					<xsl:when test="(@Type='User' or @Type='UserMulti') and ($PresenceEnabled='1')">ms-vh</xsl:when>
					<xsl:otherwise>ms-vh2</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:call-template name="dvt_headerfield">
				<xsl:with-param name="fieldname">
					<xsl:value-of select="@Name"/>
				</xsl:with-param>
				<xsl:with-param name="fieldtitle">
					<xsl:value-of select="@DisplayName"/>
				</xsl:with-param>
				<xsl:with-param name="displayname">
					<xsl:value-of select="@DisplayName"/>
				</xsl:with-param>
				<xsl:with-param name="fieldtype">
					<xsl:choose>
						<xsl:when test="@Type='Number' or @Type='Currency'">number</xsl:when>
						<xsl:otherwise>x:string</xsl:otherwise>
					</xsl:choose>
				</xsl:with-param>
			</xsl:call-template>
		</th>
		<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
		<script>
			var lastViewedCtx = new Object();			
			lastViewedCtx.ListId = "<xsl:value-of select='$List' />";
			lastViewedCtx.Url = "<xsl:value-of select='$HttpVDir' />";
		</script>
		<script>
			$(document).ready(function()
			{
				$.ajax({
					cache: false,
					url: lastViewedCtx.Url + "/_vti_bin/LastViewedService.svc/Refresh?ListId=" + lastViewedCtx.ListId,
					dataType: 'xml',
					success: function(data) {
						if(data.text == 'true')
						{
							var rowsNeedingrefresh = $("span[NullLastViewedId]");
							var ids = '';						
							$(rowsNeedingrefresh).each(function()
							{
								ids+=$(this).attr('NullLastViewedId')+",";
							});
							$.ajax({
								cache: false,
								url: lastViewedCtx.Url + "/_vti_bin/LastViewedService.svc/GetLastViewed?ListId=" + lastViewedCtx.ListId + "&amp;ids=" + ids,
								dataType: 'xml',
								success: function(data) {
									var lastVieweds = data.text.split(",");
									var rowsNeedingrefreshArray = rowsNeedingrefresh.toArray();
									for(var i in rowsNeedingrefreshArray)
									{
										$(rowsNeedingrefreshArray[i]).text(lastVieweds[i]);
									}
								}
							});
						}
					}
				});
			});
		</script>		
	</xsl:template>
</xsl:stylesheet>

